package easier.window.picker;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import easier.window.picker.PickerView.Adapter;
import easier.window.picker.PickerView.PickerItem;
import java.util.ArrayList;
import java.util.List;

public class PickerFragment< T > extends DialogFragment {

    protected PickerViewDialog dialog;
    protected CharSequence title;
    protected ActionListener< T > actionListener;
    protected List< T > list = new ArrayList<>();
    protected PickerView pickerView;
    protected Func< T > func;


    public void setActionListener( ActionListener< T > actionListener ) {

        this.actionListener = actionListener;
    }

    public void setFunc( Func< T > func ) {

        this.func = func;
    }

    public void setTitle( CharSequence title ) {

        this.title = title;
    }

    public void setList( List< T > list ) {

        this.list = list;
    }

    /**
     * @param context 创建picker
     */
    protected void createPicker( Context context ) {

        dialog = new PickerViewDialog( context );
        dialog.setContentView( R.layout.dialog_picker );
        TextView title = dialog.findViewById( R.id.title );
        pickerView = dialog.findViewById( R.id.picker );
        pickerView.setAdapter( new Adapter< PickerItem >() {
            @Override
            public int getItemCount() {

                return list.size();
            }

            @Override
            public PickerItem getItem( int index ) {

                return new PickerItem() {
                    @Override
                    public String getText() {

                        return func.accept( list.get( index ) );
                    }
                };
            }
        } );
        title.setText( this.title );
    }


    @NonNull
    @Override
    public Dialog onCreateDialog( @Nullable Bundle savedInstanceState ) {

        if ( dialog == null ) {
            createPicker( getContext() );
        }
        View yes = dialog.findViewById( R.id.done );
        View no = dialog.findViewById( R.id.close );
        attachActions( yes, no );
        return dialog;
    }

    protected void attachActions( View done, View cancel ) {

        cancel.setOnClickListener( v -> {
            if ( actionListener != null ) {
                actionListener.onCancelClick();
            }
            dismiss();
        } );
        done.setOnClickListener( v -> {
            if ( actionListener != null ) {
                int selectedItemPosition = pickerView.getSelectedItemPosition();
                actionListener.onDoneClick( list.get( selectedItemPosition ) );
            }
            dismiss();
        } );
    }

    public interface ActionListener< T > {

        void onCancelClick();

        void onDoneClick( T model );
    }


    public interface Func< T > {

        String accept( T t );
    }
}
